package system1

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"gorm.io/gorm"
)

type WorkflowOwnerService struct {
}

// CreateWorkflowOwner 创建当前审批处理人记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowOwnerService *WorkflowOwnerService) CreateWorkflowOwner(WorkflowOwner *system1.WorkflowOwner) (err error) {
	err = global.GVA_DB.Create(WorkflowOwner).Error
	return err
}

// DeleteWorkflowOwner 删除当前审批处理人记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowOwnerService *WorkflowOwnerService) DeleteWorkflowOwner(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&system1.WorkflowOwner{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&system1.WorkflowOwner{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteWorkflowOwnerByIds 批量删除当前审批处理人记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowOwnerService *WorkflowOwnerService) DeleteWorkflowOwnerByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&system1.WorkflowOwner{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&system1.WorkflowOwner{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateWorkflowOwner 更新当前审批处理人记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowOwnerService *WorkflowOwnerService) UpdateWorkflowOwner(WorkflowOwner system1.WorkflowOwner) (err error) {
	err = global.GVA_DB.Save(&WorkflowOwner).Error
	return err
}

// GetWorkflowOwner 根据ID获取当前审批处理人记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowOwnerService *WorkflowOwnerService) GetWorkflowOwner(ID string) (WorkflowOwner system1.WorkflowOwner, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&WorkflowOwner).Error
	return
}

// GetWorkflowOwnerInfoList 分页获取当前审批处理人记录
// Author [piexlmax](https://github.com/piexlmax)
func (WorkflowOwnerService *WorkflowOwnerService) GetWorkflowOwnerInfoList(info system1Req.WorkflowOwnerSearch) (list []system1.WorkflowOwner, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system1.WorkflowOwner{})
	var WorkflowOwners []system1.WorkflowOwner
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Order("created_at DESC,updated_at DESC").Find(&WorkflowOwners).Error
	return WorkflowOwners, total, err
}
